﻿using Apex.DomainCommons.Models;
using TaskMasterByQuartz.Domain.Enums;

namespace TaskMasterByQuartz.Domain.Entities
{
    public class ExecuteLog: IAggregateRoot, IEntity
    {
        /// <summary>
        /// 主键
        /// </summary>
        public Guid Id { get; protected set; } = Guid.NewGuid();
        /// <summary>
        /// 任务组
        /// </summary>
        public string JobGroup { get; set; }
        /// <summary>
        /// 任务名称
        /// </summary>
        public string JobName { get; set; }
        /// <summary>
        /// 执行内容：url、sql之类
        /// </summary>
        public string? ActionContent { get; set; }
        /// <summary>
        /// 开始执行时间
        /// </summary>
        public DateTime BeginTime { get; set; }
        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime EndTime { get; set; }
        /// <summary>
        /// 耗时（秒）
        /// </summary>
        public double ExecuteTime { get; set; }
        /// <summary>
        /// 执行结果
        /// </summary>
        public string Result { get; set; }
        /// <summary>
        /// 执行状态
        /// </summary>
        public ExecuteStatuEnum Statu { get; set; }

        /// <summary>
        /// EF CORE加载数据用
        /// </summary>
        public ExecuteLog()
        {
        }
        /// <summary>
        /// 初始化对象用
        /// </summary>
        public void Init1(string jobGroup,string jobName)
        {
            BeginTime = DateTime.Now;
            JobGroup = jobGroup;
            JobName = jobName;
        }
        public void Init2( string actionContent)
        {
            ActionContent = actionContent;
        }

        public void SetEndTime()
        {
            EndTime = DateTime.Now;
            var s = EndTime.Subtract(BeginTime).TotalSeconds;
            if (s>1)
            {
                ExecuteTime = EndTime.Subtract(BeginTime).TotalMilliseconds;
            }
            else
            {
                ExecuteTime = s;
            }
            
        }
    }
}
